const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 热搜
    popular: ['华为手机', '电脑主机', '睡衣', '硬盘', '笔记本', '手机', '5G手机', '诺基亚', '华为P30', '家具', '旅行用品', '床上用品', '日用品', '插板', '显示器', '鼠标', '键盘'],
    // 搜索记录
    lately: ['手机', '一加7Pro'],
    // 默认搜索值
    search_value: '',
    // 页面控制
    // index1: true,
    // index2: false,
    // index3: false,
    // index2页面检索结果
    // retrieval2: ['7Pro', '6T', '云耳', '7Pro', '6T', '云耳'],
    retrieval: [{
      name: '一加',
      more: ['7Pro', '6T', '云耳'],
    }, {
      name: '一加7',
      more: ['plus', '手机', '手机壳'],
    }, {
      name: '一叶子',
      // 商品平拍不超过4个字
      more: ['水润面膜', '洗面奶男', '洗面奶女'],
    }, {
      name: '一体机',
      more: ['VR', '联想', '蒸烤箱'],
    }, {
      name: '一加手机',
      more: ['充电线', '6T', '7Pro'],
    }, {
      name: '一洗黑',
      more: ['洗发水', '瑞虎', '清水'],
    }, {
      name: '一元购',
      more: ['手机', '新人', '手表'],
    }, {
      name: '一次性碗',
      more: ['自营', '塑料', '大号'],
    }],
    // 搜索类型模块
    screen_module: [
      '新品', '拼购', '品牌', '免运费', '专卖', '二手', '屏幕', 'CPU', '操作系统', '拍照'
    ],
    // result: 1,
    // 搜索结果列表
    resultList: [{
        id: 15857,
        img: "../../resources/images/product/1.jpg",
        name: "欧莱雅（LOREAL）奇焕光彩粉嫩透亮修颜霜 30ml（欧莱雅彩妆 BB霜 粉BB 遮瑕疵 隔离）",
        recommend: ['8GBRAM', '256GB', '骁龙855'],
        sale: 599,
        payNum: 2342,
        type: "小瓶超级增长长城纯纯粹粹踩踩踩踩踩",
        num: 1,
        select: 1,
        coupon: 1,
        seckill: 1,
        seckill_time: 20
      },
      {
        id: 15857,
        img: "../../resources/images/product/2.jpg",
        name: "德国DMK进口牛奶  欧德堡（Oldenburger）超高温处理全脂纯牛奶1L*12盒",
        recommend: ['罐装', '进口', '高温杀菌'],
        sale: 29,
        payNum: 999,
        type: "350克/盒",
        num: 5,
        select: 0,
        coupon: 0,
        seckill: 1,
        seckill_time: 8
      },
      {
        id: 15857,
        img: "../../resources/images/product/3.jpg",
        name: "【第2支1元】柔色尽情丝柔口红唇膏女士不易掉色保湿滋润防水 珊瑚红",
        recommend: ['玫瑰', '王', '666'],
        sale: 299,
        payNum: 666,
        type: "150g/支",
        num: 1,
        select: 1,
        coupon: 1,
        seckill: 0
      }, {
        id: 15857,
        img: "../../resources/images/product/1.jpg",
        name: "欧莱雅（LOREAL）奇焕光彩粉嫩透亮修颜霜 30ml（欧莱雅彩妆 BB霜 粉BB 遮瑕疵 隔离）",
        recommend: ['xxx', 'xxx', 'xxx'],
        sale: 599,
        payNum: 2342,
        type: "小瓶超级增长长城纯纯粹粹踩踩踩踩踩",
        num: 1,
        select: 0,
        coupon: 0,
        seckill: 1,
        seckill_time: 20
      },
      {
        id: 15857,
        img: "../../resources/images/product/2.jpg",
        name: "德国DMK进口牛奶  欧德堡（Oldenburger）超高温处理全脂纯牛奶1L*12盒",
        sale: 29,
        payNum: 999,
        type: "350克/盒",
        num: 5,
        select: 1,
        coupon: 0,
        seckill: 0,
      },
      {
        id: 15857,
        img: "../../resources/images/product/3.jpg",
        name: "【第2支1元】柔色尽情丝柔口红唇膏女士不易掉色保湿滋润防水 珊瑚红",
        sale: 299,
        payNum: 666,
        type: "150g/支",
        num: 1,
        select: 0,
        coupon: 1,
        seckill: 0
      }
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)

    console.log(this.data.retrieval)
    // 胶囊和顶部搜索框的处理
    var that = this;
    var MenuButton = app.globalData.MenuButton;
    var heard_width = MenuButton.left - 10,
      heard_margin_top = MenuButton.top;

    that.setData({
      heard_margin_top: heard_margin_top,
      heard_width: heard_width,
      width: MenuButton.width,
      height: MenuButton.height,
      top: MenuButton.top,
      left: MenuButton.left
    });

    // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
    const CurrentPages = getCurrentPages()
    console.log(CurrentPages)
    var CurrentPage = CurrentPages[(CurrentPages.length - 2)];
    if (CurrentPage) { //页面完成后删除外层if，必定通过上一个页面进入
      if (CurrentPage.route == "pages/index/index") {
        const eventChannel = this.getOpenerEventChannel()
        eventChannel.on('acceptDataFromOpenerPage', function(data) {
          that.setData({
            search_pla: data.data
          })
        })
      } else {
        that.setData({
          search_pla: '搜索全部商品'
        })
      }
    } else {
      that.setData({
        search_pla: '搜索全部商品'
      })
    }

  },

  back: function() {

    // 回调上一个页面中的事件
    // const eventChannel = this.getOpenerEventChannel()
    // eventChannel.emit('test', {
    //   data: '2'
    // });

    // 获取页面栈
    const CurrentPages = getCurrentPages()
    console.log(CurrentPages)
    var CurrentPage = CurrentPages[(CurrentPages.length - 2)];
    if (CurrentPage)
      // if (CurrentPage.route == "pages/index/index") {
      // 可通过页面栈操作上一页面的数据
      // CurrentPage.setData({
      //   res: 33
      // })
      wx.navigateBack({
        delta: 2
      })
    // }
  },
  // search: function(e) {
  //   console.log(e)
  // },
  search: function(e) {
    console.log(e);
    var that = this
    var pla = e.currentTarget.dataset.pla,
      search = e.currentTarget.dataset.search,
      target = e.target.dataset.more;

    if (target) {
      if (search && target) {
        console.log('开始搜索冒泡value:' + search + target)
        that.setData({
          search_value: search + target,
          result: 1
        })
      }
    } else if (search) {
      console.log('开始搜索value:' + search)
      that.setData({
        search_value: search,
        result: 1
      })
    } else if (pla != '搜索全部商品') {
      console.log('开始搜索pla:' + pla)
      that.setData({
        search_value: pla,
        result: 1
      })
    } else if (pla == '搜索全部商品') {
      console.log('拒绝执行搜索全部商品')
    }

  },

  search_value: function(e) {
    console.log(e.detail.value)
    if (!e.detail.value) {
      this.setData({
        search_value: e.detail.value,
        result: ''
      })
    } else {
      this.setData({
        search_value: e.detail.value,
        result: ''
      })
    }
  },
  // 置空
  empty: function(e) {
    this.setData({
      search_value: ''
    })
  },
  search2: function(e) {
    console.log(e);
    this.setData({
      result: ''
    })
  },
  search_empty: function(e) {
    console.log('清空')
    this.setData({
      result: '',
      search_value: ''
    })
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})